package com.innover.activemq.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.FileInputStream;
import java.util.Properties;

/**
 * Created by James on 8/8/2017.
 */
@Configuration
public class ActiveMQSettings {
    private static final Logger logger = LoggerFactory.getLogger(ActiveMQSettings.class);

    private String username;
    private String password;
    private String brokerUrl;
    private String standaloneTaskQueue;
    private String sequenceTaskQueue;
    private int concurrent;
    private String messageSelector;
    private boolean enableCommonConsumer;
    private boolean enableSequenceQueueConsumer;
    private boolean enableMessageSelectorConsumer;
    private int prefetch;

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getBrokerUrl() {
        return brokerUrl;
    }

    public String getStandaloneTaskQueue() {
        return standaloneTaskQueue;
    }

    public String getSequenceTaskQueue() {
        return sequenceTaskQueue;
    }

    public int getConcurrent() {
        return concurrent;
    }

    public String getMessageSelector() {
        return messageSelector;
    }

    public boolean isEnableCommonConsumer() {
        return enableCommonConsumer;
    }

    public boolean isEnableSequenceQueueConsumer() {
        return enableSequenceQueueConsumer;
    }

    public boolean isEnableMessageSelectorConsumer() {
        return enableMessageSelectorConsumer;
    }

    public int getPrefetch() {
        return prefetch;
    }

    public ActiveMQSettings(@Value(value = "${activemq-config-path}") String activeMQSettingsPath){
        try{
            Properties properties = new Properties();
            FileInputStream inputStream = new FileInputStream(activeMQSettingsPath);
            properties.load(inputStream);
            this.username = properties.getProperty("mq.username", "admin");
            this.password = properties.getProperty("mq.password", "admin");
            this.brokerUrl = properties.getProperty("mq.broker-url", "failover:tcp://127.0.0.1:61616");
            this.standaloneTaskQueue = properties.getProperty("mq.no-sequence-queue-name", "StandaloneTaskQueue");
            this.sequenceTaskQueue = properties.getProperty("mq.sequence-queue-name", "SequenceTaskQueue");
            this.concurrent = Integer.parseInt(properties.getProperty("mq.concurrent-consumer-count", "2"));
            this.messageSelector = properties.getProperty("mq.consumer-message-selector", "");
            this.enableCommonConsumer = Boolean.parseBoolean(properties.getProperty("mq.enable-common-queue-consumer", "true"));
            this.enableSequenceQueueConsumer = Boolean.parseBoolean(properties.getProperty("mq.enable-sequence-queue-consumer", "false"));
            this.enableMessageSelectorConsumer = Boolean.parseBoolean(properties.getProperty("mq.enable-message-selector-consumer", "false")) && !this.messageSelector.equals("");
            this.prefetch = Integer.parseInt(properties.getProperty("mq.prefetch", "1000"));
        } catch (Exception e){
            logger.error(e.getMessage());
            logger.warn("initial mq setting failed. will use default settings.");
            this.username = "admin";
            this.password = "admin";
            this.brokerUrl = "failover:tcp://127.0.0.1:61616";
            this.standaloneTaskQueue = "StandaloneTaskQueue";
            this.sequenceTaskQueue = "SequenceTaskQueue";
            this.concurrent = 1;
            this.messageSelector = "";
            this.enableCommonConsumer = true;
            this.prefetch = 1000;
        }
    }

}
